package cn.edu.sdjzu.knoverse.repository;

import cn.edu.sdjzu.knoverse.model.entity.kg.CaseNode;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.neo4j.repository.query.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface CaseRepository extends Neo4jRepository<CaseNode, Long> {
    
    /**
     * 管理员查询所有案例（分页）
     * @param name 案例名称（模糊查询）
     * @param type 案例类型
     * @param difficulty 案例难度
     * @param status 案例状态
     * @param pageable 分页参数
     * @return 分页案例结果
     */
    @Query(value = "MATCH (c:Case) " +
            "WHERE (CASE WHEN $name IS NULL THEN true ELSE c.name CONTAINS $name END) " +
            "AND (CASE WHEN $type IS NULL THEN true ELSE c.type = $type END) " +
            "AND (CASE WHEN $difficulty IS NULL THEN true ELSE c.difficulty = $difficulty END) " +
            "AND (CASE WHEN $status IS NULL THEN true ELSE c.status = $status END) " +
            "RETURN c " +
            "ORDER BY c.createTime DESC",
            countQuery = "MATCH (c:Case) " +
            "WHERE (CASE WHEN $name IS NULL THEN true ELSE c.name CONTAINS $name END) " +
            "AND (CASE WHEN $type IS NULL THEN true ELSE c.type = $type END) " +
            "AND (CASE WHEN $difficulty IS NULL THEN true ELSE c.difficulty = $difficulty END) " +
            "AND (CASE WHEN $status IS NULL THEN true ELSE c.status = $status END) " +
            "RETURN count(c)")
    Page<CaseNode> findByConditionsForAdmin(
            @Param("name") String name,
            @Param("type") String type,
            @Param("difficulty") Integer difficulty,
            @Param("status") Integer status,
            Pageable pageable);
    
    /**
     * 普通用户查询案例（分页）
     * @param userId 当前用户ID
     * @param name 案例名称（模糊查询）
     * @param type 案例类型
     * @param difficulty 案例难度
     * @param status 案例状态
     * @param pageable 分页参数
     * @return 分页案例结果
     */
    @Query(value = "MATCH (c:Case) " +
            "WHERE (c.createById = $userId OR c.status = 2) " +
            "AND (CASE WHEN $name IS NULL THEN true ELSE c.name CONTAINS $name END) " +
            "AND (CASE WHEN $type IS NULL THEN true ELSE c.type = $type END) " +
            "AND (CASE WHEN $difficulty IS NULL THEN true ELSE c.difficulty = $difficulty END) " +
            "AND (CASE WHEN $status IS NULL THEN true ELSE c.status = $status END) " +
            "RETURN c " +
            "ORDER BY c.createTime DESC",
            countQuery = "MATCH (c:Case) " +
            "WHERE (c.createById = $userId OR c.status = 2) " +
            "AND (CASE WHEN $name IS NULL THEN true ELSE c.name CONTAINS $name END) " +
            "AND (CASE WHEN $type IS NULL THEN true ELSE c.type = $type END) " +
            "AND (CASE WHEN $difficulty IS NULL THEN true ELSE c.difficulty = $difficulty END) " +
            "AND (CASE WHEN $status IS NULL THEN true ELSE c.status = $status END) " +
            "RETURN count(c)")
    Page<CaseNode> findByConditionsForUser(
            @Param("userId") Long userId,
            @Param("name") String name,
            @Param("type") String type,
            @Param("difficulty") Integer difficulty,
            @Param("status") Integer status,
            Pageable pageable);
    
    /**
     * 根据ID查询案例
     * @param nodeId 案例ID
     * @return 案例实体
     */
    @Query("MATCH (c:Case) WHERE c.nodeId = $nodeId RETURN c")
    CaseNode findCaseById(Long nodeId);
    
    /**
     * 根据ID和用户ID查询案例
     * @param id 案例ID
     * @param userId 用户ID
     * @return 案例实体
     */
    @Query("MATCH (c:Case) WHERE c.nodeId = $id AND (c.createById = $userId OR c.status = 2) RETURN c")
    CaseNode findByIdAndUserId(@Param("id") Long id, @Param("userId") Long userId);
    
    /**
     * 获取案例类型列表
     * @return 类型列表
     */
    @Query("MATCH (c:Case) RETURN DISTINCT c.type")
    List<String> findAllTypes();
} 